package com.ztewatch.wearcontacts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.ztewatch.wearcontacts.presenter.LoadCursorDataPresenter;
import com.ztewatch.wearcontacts.view.BaseGridViewAdapter;
import com.ztewatch.wearcontacts.view.ContactCursorAdapter;
import com.ztewatch.wearcontacts.view.ILoadCursorDataView;
import com.ztewatch.wearcontacts.view.ScrollGridView;

public class WearContactMainActivity extends Activity implements
		ILoadCursorDataView {
	private ScrollGridView base_gridview;
	private List<HashMap<String, Object>> baseDataList = new ArrayList<HashMap<String, Object>>();
	private ScrollGridView contact_gridview;
	private ContactCursorAdapter contactCursorAdapter;
	private LoadCursorDataPresenter mLoadCursorDataPresenter;
	private boolean save_contact_flag = false;
//	private AdViewStream mAdviewStream;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wear_contact_main);
		initBaseGridView();
		initContactGridView();
		mLoadCursorDataPresenter = new LoadCursorDataPresenter(this, this,
				contactCursorAdapter);
	}

//	private void initAdviewStream() {
//		if (mAdviewStream != null)
//			mAdviewStream.setClosed(true);
//		mAdviewStream = (AdViewStream) findViewById(R.id.adview_ayout);
//		mAdviewStream.setAdViewInterface(new AdViewInterface() {
//
//			@Override
//			public void onDisplayAd() {
//
//			}
//
//			@Override
//			public void onClosedAd() {
//
//			}
//
//			@Override
//			public void onClickAd() {
//
//			}
//		});
//	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		save_contact_flag = intent.getBooleanExtra(
				WearContactEditActivity.SAVE_CONTACT_FLAG, false);
	}

	private void initContactGridView() {
		contact_gridview = (ScrollGridView) findViewById(R.id.contact_gridview);
		contactCursorAdapter = new ContactCursorAdapter(this, null);
		contact_gridview.setAdapter(contactCursorAdapter);
		contact_gridview.setOnItemClickListener(new ContactItemClickListener());
		contact_gridview.setSelected(true);
	}

	private void initBaseGridView() {
		base_gridview = (ScrollGridView) findViewById(R.id.base_gridview);
		HashMap<String, Object> itemHashMap = new HashMap<String, Object>();
		itemHashMap.put(BaseGridViewAdapter.ITEM_IMAGE,
				R.drawable.ic_add_contact);
		itemHashMap.put(BaseGridViewAdapter.ITEM_TEXT, getResources()
				.getString(R.string.base_newcontact));
		baseDataList.add(itemHashMap);

		HashMap<String, Object> itemHashMap1 = new HashMap<String, Object>();
		itemHashMap1.put(BaseGridViewAdapter.ITEM_IMAGE, R.drawable.ic_edit);
		itemHashMap1.put(BaseGridViewAdapter.ITEM_TEXT, getResources()
				.getString(R.string.base_edit));
		baseDataList.add(itemHashMap1);

		HashMap<String, Object> itemHashMap2 = new HashMap<String, Object>();
		itemHashMap2.put(BaseGridViewAdapter.ITEM_IMAGE, R.drawable.ic_sort);
		itemHashMap2.put(BaseGridViewAdapter.ITEM_TEXT, getResources()
				.getString(R.string.base_Sort));
		baseDataList.add(itemHashMap2);

		HashMap<String, Object> itemHashMap3 = new HashMap<String, Object>();
		itemHashMap3.put(BaseGridViewAdapter.ITEM_IMAGE, R.drawable.ic_folder);
		itemHashMap3.put(BaseGridViewAdapter.ITEM_TEXT, getResources()
				.getString(R.string.base_Folder));
		baseDataList.add(itemHashMap3);
		BaseGridViewAdapter baseGridViewAdapter = new BaseGridViewAdapter(this,
				baseDataList);
		base_gridview.setAdapter(baseGridViewAdapter);
		base_gridview.setOnItemClickListener(new BaseItemClickListener());
	}

	private class ContactItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// if (position == 0) {// History
			// return;
			// }
			Cursor cursor = (Cursor) contactCursorAdapter.getItem(position);
			if (cursor != null) {
				// int is_group_index = cursor
				// .getColumnIndex(SortContactsAndGroupLoader.IS_GROUP);
				// if (is_group_index != -1) {
				// int is_group = cursor.getInt(is_group_index);
				// if (is_group == 0) {
				Uri contactUri = getContactUri(cursor);
				if (contactUri != null) {// contact
					Intent intent = new Intent(WearContactMainActivity.this,
							WearContactDisplayActivity.class);
					intent.setData(contactUri);
					startActivity(intent);
				}
				// } else {
				// int groupId_index = cursor.getColumnIndex(Groups._ID);
				// if (groupId_index != -1) {
				// long groupId = cursor.getLong(groupId_index);
				// Uri group_uri = getGroupUriFromId(groupId);
				// Intent intent = new Intent(
				// WearContactMainActivity.this,
				// WearContactGroupDetailActivity.class);
				// int title_index = cursor
				// .getColumnIndex(Contacts.DISPLAY_NAME_PRIMARY);//
				// Groups.TITLE
				// if (title_index != -1) {
				// String title = cursor.getString(title_index);
				// intent.putExtra(
				// WearContactGroupListActivity.GROUP_TITLE,
				// title);
				// }
				// intent.setData(group_uri);
				// startActivity(intent);
				// }
				// }
				// } else {
				// Uri contactUri = getContactUri(cursor);
				// if (contactUri != null) {// contact
				// Intent intent = new Intent(
				// WearContactMainActivity.this,
				// WearContactDisplayActivity.class);
				// intent.setData(contactUri);
				// startActivity(intent);
				// }
				// }
			}
		}

	}

	private class BaseItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (position) {
			case 0:// new contact
				if (getResources().getBoolean(R.bool.config_LGU_flag)) {
					Intent intent = new Intent();
					intent.setClass(WearContactMainActivity.this,
							WearContactEditActivity.class);
					startActivity(intent);
				} else {
					Intent intent = new Intent();
					intent.setAction(PasswordkeypadActivity.NEW_CONTACT_ACTION);
					intent.setClass(WearContactMainActivity.this,
							PasswordkeypadActivity.class);
					startActivity(intent);
				}
				break;
			case 1:// edit contact
				if (getResources().getBoolean(R.bool.config_LGU_flag)) {
					Intent intent = new Intent();
					intent.setClass(WearContactMainActivity.this,
							WearContactEditMainActivity.class);
					startActivity(intent);
				} else {
					Intent intent1 = new Intent();
					intent1.setAction(PasswordkeypadActivity.EDIT_CONTACT_ACTION);
					intent1.setClass(WearContactMainActivity.this,
							PasswordkeypadActivity.class);
					startActivity(intent1);
				}
				break;
			case 2:// sort
				Intent intent2 = new Intent();
				// intent2.setAction(PasswordkeypadActivity.SORT_CONTACT_ACTION);
				// intent2.setClass(WearContactMainActivity.this,
				// PasswordkeypadActivity.class);
				intent2.setClass(WearContactMainActivity.this,
						WearContactSortActivity.class);
				startActivity(intent2);
				break;
			case 3:// folder
				if (getResources().getBoolean(R.bool.config_LGU_flag)) {
					Intent intent = new Intent();
					intent.setClass(WearContactMainActivity.this,
							WearContactGroupListActivity.class);
					startActivity(intent);
				} else {
					Intent intent3 = new Intent();
					intent3.setAction(PasswordkeypadActivity.FOLDER_CONTACT_ACTION);
					intent3.setClass(WearContactMainActivity.this,
							PasswordkeypadActivity.class);
					startActivity(intent3);
				}
				break;
			default:
				break;
			}
		}

	}

	public Uri getContactUri(Cursor cursor) {
		Uri uri = null;
		int id_index = cursor.getColumnIndex(Contacts._ID);
		int lookup_key_index = cursor.getColumnIndex(Contacts.LOOKUP_KEY);
		if (id_index != -1 && lookup_key_index != -1) {
			long contactId = cursor.getLong(id_index);
			String lookupKey = cursor.getString(lookup_key_index);
			uri = Contacts.getLookupUri(contactId, lookupKey);
		}
		return uri;
	}

//	private Uri getGroupUriFromId(long groupId) {
//		return ContentUris.withAppendedId(Groups.CONTENT_URI, groupId);
//	}

	@Override
	protected void onStart() {
		super.onStart();
//		initAdviewStream();
		mLoadCursorDataPresenter.restartLoader(0);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mLoadCursorDataPresenter.destroyLoader(0);
	}

	@Override
	public void getLoadFinishCursor(Cursor c) {
		contactCursorAdapter.swapCursor(c);
		mLoadCursorDataPresenter.updateSort(c);
		int count = c.getCount();
		if (save_contact_flag && count > 1) {
			contact_gridview.setSelection(count - 1);
		}
	}
}
